package com.attempt.grab.config;

import com.attempt.grab.interceptor.WebSocketInterceptor;
import com.attempt.grab.service.GrabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author xuhaifeng
 * @since 2022年02月25日 下午 1:22
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    private final GrabService service;

    @Autowired
    public WebSocketConfig(GrabService service) {this.service = service;}

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(service, "/service")
                .addInterceptors(new WebSocketInterceptor())
                .setAllowedOrigins("*");
    }
}
